package com.hsogoo.study.concurrency;

import org.junit.Test;

/**
 * Created by za-huangsaigang on 2018/8/27.
 */
public class ReOrderingTest {

    private int x = 0, y = 0, a = 0, b = 0;

    @Test
    public void testReOrdering() throws InterruptedException {

        for (int i = 0; i < 100; i++) {
            x = y = a = b = 0;
            Thread one = new Thread() {
                public void run() {
                    a = 1;
                    x = b;
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            Thread two = new Thread() {
                public void run() {
                    b = 1;
                    y = a;
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
            one.start();
            two.start();
            one.join();
            two.join();
            System.out.println(x + "" + y);
        }
    }

}
